package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.ProductAttributeApi;
import com.yoda.mall.model.entity.pms.ProductAttribute;
import com.yoda.mall.model.entity.pms.ProductAttributeCategory;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.ProductAttributeCategoryMapper;
import com.yoda.mall.mysql.mapper.ProductAttributeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangjia
 * @date 2022/12/17
 * @Description
 */
@DubboService
public class ProductAttributeApiImpl implements ProductAttributeApi {

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Resource
    private ProductAttributeCategoryMapper productAttributeCategoryMapper;

    //添加商品属性分类
    @Override
    public Boolean addProduct(String name) {
        ProductAttributeCategory category = new ProductAttributeCategory();
        category.setName(name);
        int count = productAttributeCategoryMapper.insert(category);
       return count != 0;
    }

    //根据id删除
    @Override
    public void delete(Long id) {
        productAttributeCategoryMapper.deleteById(id);
    }


    //获取单个商品属性分类信息
    @Override
    public ProductAttributeCategory findById(Long id) {
      return productAttributeCategoryMapper.selectById(id);
    }

    //修改
    @Override
    public void update(Long id, String name) {
        ProductAttributeCategory category = new ProductAttributeCategory();
        category.setId(id);
        category.setName(name);
        productAttributeCategoryMapper.updateById(category);
    }

    //分页查询
    @Override
    public PageResult<ProductAttributeCategory> list(Integer pageNum, Integer pageSize) {
        Page<ProductAttributeCategory> page = new Page<>(pageNum,pageSize);
        productAttributeCategoryMapper.selectPage(page,null);
        return new PageResult<>(pageNum,pageNum,(int)page.getTotal(),page.getRecords());
    }

    //查询所有
    @Override
    public List<ProductAttributeCategory> findAll() {
       return productAttributeCategoryMapper.selectList(new LambdaQueryWrapper<>());
    }

    //根据商品分类的id获取商品属性及属性分类
    @Override
    public ProductAttribute findAttribute(Long productCategoryId) {
      return productAttributeMapper.selectById(productCategoryId);
    }


}
